require(['./config'], () => {
    require(['request', 'template', 'header', 'footer'], (req, template) => {
        class Shop {
            constructor() {
                this.getProdId()
                
            }
            getProdId() {
                // console.log(window)
                let str = window.location.search
                str = str.split('=')[1]
                // console.log(str)
                this.getShopData(str)
            }
            getShopData(prodId) {
                req.getShopBasktData(prodId)
                    .then(res => {
                        if (res.code === 200) {
                            const { categories, items } = res.data
                            this.createTabs(categories)
                            this.createList(items)
                            this.bindEvent()
                        }
                    })
            }
            // 渲染旁侧列表
            createTabs(categories) {
                const html = $('#collection_menu_shelf_top').html()
                $('#collection_menu_shelf_top').html(html + template('listTabsTemplate', { categories }))
            }
            createList(prodList) {
                const { list } = prodList
                $('.products_content').html(template('prodListTemplate', { list }))
            }
            // 商品加减按钮
            bindEvent(){
                var up = document.getElementsByClassName('add-up')
                // console.log(up)
                var down= document.getElementsByClassName('add-down')
                var count = document.getElementsByClassName('txt-amount')
                for(let i = 0;i < up.length;i++){
                    up[i].onclick = function(){
                        count[i].value++
                    }
                    down[i].onclick = function(){
                        if(count[i].value - 0 > 1){
                            count[i].value--
                        }else{
                            count[i].value = 1
                        }
                    }
                }
                // $('.add-down').onclick = function(){
                //     let num = $('.txt-amount').val()
                //     if(num > 1){
                //         num--
                //     }else{
                //         num = 1
                //     }
                // }
                // $('.add-up').onclick = function(){
                //     let num = $('.txt-amount').val()
                //     num++
                // }
            }
        }
        return new Shop()
    })
})
